package C042;

/**
 * ClassName: C04
 * Package: C042
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/2 22:22
 * @Version 1.0
 * @Since 1.0
 */
public class C04 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if(!(operator(i) == operator2(i))) {
                System.out.println("error !!!");
                return;
            }
            System.out.println(i + ": " + operator(i));
        }
    }

    public static int operator(int n) {
        char[] arr = new char[n];
        return process(arr, 0);
    }

    public static int operator2(int n) {
        if(n == 0 || n == 1) {
            return 0;
        }
        if(n == 2) {
            return 3;
        }
        if(n == 3) {
            return 18;
        }
        return (n + 1) * 6 ;
    }

    public static int process(char[] arr, int index) {
        if (index == arr.length) {
            int cnt = 0;
            for (int i = 0; i < arr.length; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (isReverse(arr, i, j)) {
                        cnt ++;
                    }
                    if(cnt > 1) {
                        return 0;
                    }
                }
            }
            return cnt == 1 ? 1 : 0;
        } else {
            arr[index] = 'r';
            int num1 = process(arr, index + 1);
            arr[index] = 'e';
            int num2 = process(arr, index + 1);
            arr[index] = 'd';
            int num3 = process(arr, index + 1);
            return num1 + num2 + num3;
        }
    }

    private static boolean isReverse(char[] arr, int i, int j) {
        while (i < j) {
            if(arr[i] != arr[j]) {
                return false;
            }
            i ++;
            j --;
        }
        return true;
    }
}
